import { WeaInputLocale} from 'ecCom';
import _ from 'lodash';
const getMultiStrFromBase64 = WeaInputLocale.getMultiStrFromBase64
export const getColumns=(columns,optionsMap,browserMap,isfrontmultlang,tableStore?,sqlneedkey?)=>{
    return columns.map(item=>{
        let conditionType='INPUT';
        let title='';
        let key='';
        let otherParams={};
        title=item.fieldlabel;
        if(item.fieldid=='-1'){
            key='modedatacreatedate';
        }else if(item.fieldid=='-2'){
            key='modedatacreaterspan';
            conditionType='htmlinput';
        }else if(item.fieldid=='-3'){
            key='id';
        }else if(item.fieldid=='-4'){
            key='modelableidspan';
        }else if(item.fieldid=='-5'){
            key='modedatastatusspan';
        }else if(item.fieldid=='-8'){
            key='modedatamodifydatetime';
        }else if(item.fieldid=='-7'){
            key='modedatamodifierspan';
            conditionType='htmlinput';
        }else{
            key=item.fieldname;
        }

        const fieldhtmltype=item.fieldhtmltype;
        const fielddbtype=item.fielddbtype;
        const type=item.type;
        let options=[];
        let detailtype='1';
        let  width=100;
        let reg:RegExp=new RegExp('');
        let browserConditionParam={};
        let uploadConditionParam={};
        if(fieldhtmltype=='1'){//单行文本
            if(type=='2'){//整数
                reg=/^-?\d+$/;
                detailtype='2';
                otherParams={max:2147483647,min:-2147483648};
            }else if(type=='3'||type=='4'||type=='5'){//浮点数 金额转换 金额千分位
                reg=/^(-?\d+)(.[0-9]{0,2})?$/;
                switch(item.qfws){
                    case 1:reg=/^(-?\d+)(.[0-9]{0,1})?$/;break;
                    case 2:reg=/^(-?\d+)(.[0-9]{0,2})?$/;break;
                    case 3:reg=/^(-?\d+)(.[0-9]{0,3})?$/;break;
                    case 4:reg=/^(-?\d+)(.[0-9]{0,4})?$/;break;
                    case 5:reg=/^(-?\d+)(.[0-9]{0,5})?$/;break;
                    case 6:reg=/^(-?\d+)(.[0-9]{0,6})?$/;break;
                    case 7:reg=/^(-?\d+)(.[0-9]{0,7})?$/;break;
                    case 8:reg=/^(-?\d+)(.[0-9]{0,8})?$/;break;
                    case 9:reg=/^(-?\d+)(.[0-9]{0,9})?$/;break;
                    case 10:reg=/^(-?\d+)(.[0-9]{0,10})?$/;break;
                    case 11:reg=/^(-?\d+)(.[0-9]{0,11})?$/;break;
                    case 12:reg=/^(-?\d+)(.[0-9]{0,12})?$/;break;
                    case 13:reg=/^(-?\d+)(.[0-9]{0,13})?$/;break;
                    case 14:reg=/^(-?\d+)(.[0-9]{0,14})?$/;break;
                    case 15:reg=/^(-?\d+)(.[0-9]{0,15})?$/;break;
                }
                detailtype='2';
                key+='span';
                otherParams={max:999999999999.999999999999,min:-999999999999.999999999999};
            }
        }else if (fieldhtmltype == '5') {//选择框
            options=optionsMap[item.fieldid];
            conditionType='SELECT';
            detailtype=type;
        }else if (fieldhtmltype == '3') {//浏览按钮
            if(type=='2'){
                conditionType='date';
            }else if(type=='19'){
                conditionType='time';
            }else if(type=='290'){
                conditionType='datetime';
            }else{
                conditionType='browser';
                if (type&&(type == '4' ||type == '57'||type == '164'  ||type == '169'||type == '194'|| type == '167'|| type == '170'|| type == '168')){   //部门，多部门，分部，多分部，单点分权，分权单部门，分权多分部，分权多部门
                    browserMap[item.fieldid].dataParams = {...browserMap[item.fieldid].dataParams,fromModule:"model" }
                    browserMap[item.fieldid].destDataParams = {...browserMap[item.fieldid].destDataParams,fromModule:"model" }
                }
                browserConditionParam=browserMap[item.fieldid];
            }
        }else if (fieldhtmltype == '2') {//多行文本
            if(type=='1'){
                conditionType='textarea';
                key+='span';
            }else if(type == "2"){
                conditionType='htmlinput';
            }

        }else if (fieldhtmltype == '4') {//Check框
            conditionType='checkbox';
        }else if(fieldhtmltype == '6'){
                conditionType='weaupload';
                width+=60;
                uploadConditionParam=item.uploadMap;
        }else if(fieldhtmltype == '7'){//特殊字段
            key=item.fieldname+'span';
            conditionType='htmlinput'
        }
        return {
            ...otherParams,
            title: title,
            key: key,
            width: width,
            reg:reg,
            conditionType: conditionType,
            detailtype: detailtype,
            viewAttr: item.editable=='1'?2:1,
            fixed: false,
            browserConditionParam:browserConditionParam,
            uploadConditionParam:uploadConditionParam,
            options:options,
            fieldid:item.fieldid,
            qfw:item.qfws,
            fieldhtmltype:fieldhtmltype,
            fielddbtype:fielddbtype,
            fieldtype:type,
            textheight:item.textheight,
            onChange:(v,row)=>{
                const value=tableStore.form.$('tableForm').$(row.key).$(key).value;
                let oldValue=tableStore.form.$('tableForm').$(row.key).$(key).oldValue;
                if(fieldhtmltype=='4'&&oldValue=='')oldValue='0';
                let ischange=value!=oldValue;
                if(isfrontmultlang&&fieldhtmltype=='1'&&type=='1'){
                    ischange=value!=getMultiStrFromBase64(oldValue);
                }
                if(ischange){
                    tableStore.form.$(`tableForm.${row.key}.${key}`).modified=true;
                }else{
                    tableStore.form.$(`tableForm.${row.key}.${key}`).modified=false;
                }
                if(sqlneedkey){
                    getSqlNeedFieldKey(v,key,item.detailtable,sqlneedkey,tableStore,row.key,item.fieldid);
                }
            }
        };
    });
}

export const getSqlNeedFieldKey=(v,key,detailtable,sqlneedkey,tableStore,rowkey,fieldid)=>{
    const currenttime = new Date().getTime();
    _.forEach(sqlneedkey, function (value,fieldids) {
        const fields: any = { currenttime, };
        _.forEach(value, function (value1, sql) {
            let keytemp = key
            if (_.parseInt(sql) % 2 === 0) {//主表字段
                if(key.indexOf("span")!= -1){
                    keytemp=keytemp.replace("span","");
                    //fieldids=fieldids+"span";
                }
                if(keytemp===value1){
                    const oldbrowserProps = tableStore.form.$('tableForm').$(rowkey).$(fieldids).browserProps ||{};
                    _.set(fields, `${value1}_${currenttime}`, v);
                    _.set(fields, 'field'+fieldid, v);
                    tableStore.form.$('tableForm').$(rowkey).$(fieldids).browserProps={
                        dataParams :{...oldbrowserProps.dataParams,...fields},
                        conditionDataParams :{...oldbrowserProps.conditionDataParams,...fields},
                        completeParams :{...oldbrowserProps.completeParams,...fields},
                    }
                }
            }else{
                var valuetemp = "d_" + value1;
                if(key.indexOf("span")!= -1){
                    keytemp=keytemp.replace("span","");
                    //fieldids=fieldids+"span";
                }
                if(keytemp===valuetemp){
                    const oldbrowserProps = tableStore.form.$('tableForm').$(rowkey).$(fieldids).browserProps ||{};
                    _.set(fields, `${detailtable + "_" +value1}_${currenttime}`, v);
                    _.set(fields, 'field'+fieldid, v);
                    tableStore.form.$('tableForm').$(rowkey).$(fieldids).browserProps={
                        dataParams :{...oldbrowserProps.dataParams,...fields},
                        conditionDataParams :{...oldbrowserProps.conditionDataParams,...fields},
                        completeParams :{...oldbrowserProps.completeParams,...fields},
                    }
                }
            }
        });
    });
}
